<template>
  <page-form :x-uri="uri" :bind-data="datas" :custom-components="custom" :bind-rules="rules" :bind-submit="submit" :disable-submit-btn="disableSubmitBtn">
    <!-- 自定义tab -->
    <template v-slot:page-tabs>
      <tabs :tabs="tabs" />
    </template>
  </page-form>
</template>

<script>
import PageForm from '@public/template/page_form.vue';
import ChooseImage from '@/pages/material/choose/image.vue';
import { mapState } from 'vuex';
import Tabs from './tabs';
export default {
  components: {
    PageForm,
    Tabs
  },
  data() {
    return {
      uri: '/school/admin/create',
      tabs: [
        {
          title: '新建机构',
          uri: '/school/create',
          meta: {
            permission: 'school.create'
          }
        }
      ],
      datas: {
        status: 1,
        logo: 0
      },
      custom: {
        logo: ChooseImage,
        legal_IDcard_positive: ChooseImage,
        legal_IDcard_side: ChooseImage,
        business_license: ChooseImage
      },
      rules: {
        logo: [{ required: true, message: '请选择LOGO', trigger: ['blur', 'change'] }],
        admin_user_phone: [
          { required: true, message: '请输入手机号', trigger: ['blur', 'change'] },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入正确的手机号',
            trigger: ['blur', 'change']
          }
        ]
      },
      disableSubmitBtn: false
    };
  },
  computed: {
    ...mapState('pageForm', {
      pageData: 'responseData'
    })
  },
  methods: {
    submit: function(datas) {
      this.$http.send(
        this.pageData.saveMethod,
        this.pageData.savePostUrl,
        datas,
        response => {
          if (response.data.created == 1) {
            const message = '<p>已为当前机构创建了默认机构管理员账号，账号如下</p><p>账号: ' + datas.admin_user_phone + '</p><p>密码: 账号后6位</p>';
            this.$box
              .success(message, '自动创建账号提示', {
                dangerouslyUseHTMLString: true
              })
              .then(() => {
                this.$router.push({ path: '/school/index' });
              })
              .catch(() => {});
          } else {
            this.$message.success(response.msg, _ => {
              this.$router.push({ path: '/school/index' });
            });
          }
        },
        response => {
          this.disableSubmitBtn = false;
          this.$message.error(response.msg);
        }
      );
    }
  }
};
</script>
